using ED.Common;
using ED.Common.BaseModel;
using ED.Common.Extensions;
using ED.Entity;
using ED.IRepository;
using ED.IService;
using ED.Model.Request;
using ED.Model.Response;

/// <summary>
/// CityManage服务
/// </summary>
namespace ED.Service
{
    public class CityManageService : BaseService, ICityManageService
    {
        private readonly ICityManageRepository citymanageRepository;
        public CityManageService(ICityManageRepository citymanageRepository)
        {
            this.citymanageRepository = citymanageRepository;
        }

        /// <summary>
        /// CityManage添加
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Add(BaseCityoperateReq req)
        {
            // 字段校验
            var entity = citymanageRepository.Get(t => t.Name == req.Name && t.Code == req.Code);
            if (!entity.IsDefault())
            {
                ThrowHelp.Throw("该省份、城市或者地区已存在");
            }
           var cityEntity = req.Mapper<Base_CityEntity>();
           var res =  citymanageRepository.Insert(cityEntity);
            return !res.IsDefault() ? new Tuple<bool, string>(true, "添加成功") : new Tuple<bool, string>(false, "添加失败");
        }

        /// <summary>
        /// CityManage删除
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Delete(int req)
        {
            if (req <= 0)
            {
                ThrowHelp.Throw("该城市编号不存在");
            }
            var childList = citymanageRepository.GetList(t => t.ParentId == req);
            int res = 0;
            Orm.Transaction(() =>
            {
                if (childList.IsNotNullOrEmpty())
                {
                    citymanageRepository.Delete(childList.Select(t => t.Id));
                }

                res = citymanageRepository.Delete(req);
            });
            return res > 0 ? new Tuple<bool, string>(true, "删除成功") : new Tuple<bool, string>(false, "删除失败");
        }

        /// <summary>
        /// CityManage查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageOut<BaseCityRes> Page(BaseCityReq req)
        {

            var cityList = Orm.Select<Base_CityEntity>().WhereIf(req.CityName.IsNotNullOrEmptyOrWhiteSpace(), t => t.Name.Contains(req.CityName))
                .ToTreeList();

            return new PageOut<BaseCityRes> { List = cityList.Mapper<List<BaseCityRes>>(), TotalCount = -1 };
        }

        /// <summary>
        /// CityManage更新
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public Tuple<bool, string> Update(BaseCityoperateReq req)
        {
            // 字段校验
            var entity = citymanageRepository.Get(t => t.Id == req.Id);
            if (entity.IsDefault())
            {
                ThrowHelp.Throw("该省份、城市或者地区不存在");
            }
            entity = req.Mapper<Base_CityEntity>();
            var res = citymanageRepository.Update(entity);
            return !res.IsDefault() ? new Tuple<bool, string>(true, "更新成功") : new Tuple<bool, string>(false, "更新失败");
        }
    }
}
